
//  DailyEventCounterDelegate.m
//  SecretRoom
//
//  Created by Sitdhibhong Laokok on 9/3/54 BE.
//  Copyright 2554 App Tree. All rights reserved.
//

#import "DailyEventCounterDelegate.h"

@implementation DailyEventCounterDelegate

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (int)numberOfEventWithCategory:(kSREventCategory)category inManagedObjectContex:(NSManagedObjectContext *)context
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:context];
    NSSortDescriptor *sortByDate = [[NSSortDescriptor alloc] initWithKey:@"eventTimestamp" ascending:NO];
    
    request.sortDescriptors = [NSArray arrayWithObjects:sortByDate, nil];
    [sortByDate release];
    
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    gregorian.locale = [NSLocale systemLocale];
    NSDateComponents *yearMonthDayComponents = [gregorian components:(NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit) fromDate:[SLCommonsDateUtility currentGMTDate]];
    NSDateComponents *yearMonthTomorrowComponents = [yearMonthDayComponents copy];
    yearMonthTomorrowComponents.day++;
    
    switch (category) {
        case kSREventCategoryInbox:
            request.predicate = [NSPredicate predicateWithFormat:@"eventTimestamp = nil AND isArchived = %@", [NSNumber numberWithBool:NO]];
            break;
            
        case kSREventCategoryOverDue:
            request.predicate = [NSPredicate predicateWithFormat:@"eventTimestamp < %@ AND isArchived = %@", [gregorian dateFromComponents:yearMonthDayComponents], [NSNumber numberWithBool:NO]];
            break;                         
            
        case kSREventCategoryToday:
            request.predicate = [NSPredicate predicateWithFormat:@"eventTimestamp >= %@ AND eventTimestamp < %@ AND isArchived = %@", [gregorian dateFromComponents:yearMonthDayComponents], [gregorian dateFromComponents:yearMonthTomorrowComponents], [NSNumber numberWithBool:NO]];
            break;
            
        case kSREventCategoryLogBook:
            request.predicate = [NSPredicate predicateWithFormat:@"isArchived = %@", [NSNumber numberWithBool:YES]];
            break;
            
        case kSREventCategorySchedule:
            request.predicate = [NSPredicate predicateWithFormat:@"eventTimestamp >= %@ AND isArchived = %@", [gregorian dateFromComponents:yearMonthTomorrowComponents], [NSNumber numberWithBool:NO]];
            break;
            
        default:
            break;
    }
    
    NSLog(@"Predicate: %@", request.predicate);
    
    NSError *error = nil;
    NSArray *eventRecords = (request.predicate == nil) ? nil : [context executeFetchRequest:request error:&error] ;
    
    if (error != nil) {
#ifdef DEBUG
        NSLog(@"Error exists: %@", [error localizedDescription]);
        [error release];
#endif
    }
    
    [gregorian release];
    [request release];
    [yearMonthTomorrowComponents release];
    
    return (eventRecords == nil) ? 0 : [eventRecords count] ;
}

@end
